package com.copa.module.user.controller;

//import com.copa.config.WebSecurityConfig;
import com.copa.config.WebSecurityConfig;
import com.copa.domain.ResponseDto;
import com.copa.module.user.domain.dto.UserLoginDTO;
import com.copa.module.user.domain.vo.UserLoginVO;
import com.copa.module.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author copa
 * @createDate 2021-01-05 15:06
 * @function
 */
@RestController
@Api(tags = {"用户登录验证"})
public class AuthController {

    @Autowired
    private UserService userService;

    @PostMapping("/auth/login")
    @ApiOperation(value = "后台登录")
    public ResponseDto<UserLoginDTO> login(@RequestBody UserLoginVO userLoginVO, HttpServletResponse response) {

        ResponseDto<UserLoginDTO> loginRes = userService.login(userLoginVO);
        if (loginRes.isSuccess()) {
            //将Token写入到Http头部
            response.addHeader(WebSecurityConfig.AUTHORIZATION_HEADER, "Bearer " + loginRes.getData().getToken());
        }
        return loginRes;
    }
}
